/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.programacao.concorrente.monitor;

import br.com.programacao.concorrente.problema.Buffer;
import br.com.programacao.concorrente.auxiliar.Constantes;
import java.awt.Color;
import java.awt.Component;
import javax.swing.JProgressBar;

/**
 * Frame responsavel por apresentar as informações do que está sendo executado
 * @author afonso.rodrigues
 */
public class Tela extends javax.swing.JFrame implements Monitor{

    /**
     * Creates new form Tela
     */
    private final Buffer buffer;
    private JProgressBar jProgressBar;
    public Tela(Buffer buffer) {
        this.buffer = buffer; 
        initComponents();
        initJProgressBar();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLSize = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLTime = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLSize.setText("Quantidade");

        jLabel1.setText("Tempo de execução:");

        jLTime.setText("jLabel1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLSize)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 68, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLTime))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLSize)
                    .addComponent(jLabel1)
                    .addComponent(jLTime, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 90, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLSize;
    private javax.swing.JLabel jLTime;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables

    @Override
    public void atualizar() {        
        jProgressBar.setValue(Math.round((this.buffer.getSize()*100)/Constantes.TAM_MAX_BUFFER));
        jLSize.setText(String.valueOf(this.buffer.getSize())+ " Ultimo:"+Constantes.LAST_NAME_FILE_GENERATE);
        jLTime.setText(String.valueOf(Constantes.getTimeMillis() - Constantes.INSTANT_INIT));
        jLabel1.setText(String.valueOf(((Constantes.getTimeMillis() - Constantes.INSTANT_INIT) < Constantes.TIME_EXECUTION)));
    }

    private void initJProgressBar() {
        jProgressBar = new JProgressBar();
        jProgressBar.setBounds(getHeight()/4, getWidth()/10, 200, 20);  
         jProgressBar.setForeground(Color.BLACK);  
         jProgressBar.setAlignmentX(Component.CENTER_ALIGNMENT);
         jProgressBar.setAlignmentY(Component.CENTER_ALIGNMENT);
         jProgressBar.setStringPainted(true);
         getContentPane().add(jProgressBar);  
        //jPanel.add(jProgressBar);
    }
}
